/*******************************************************************************
 * Copyright (c) 2010, 2015 git@git.oschina.net:kaiwill/springstage.git
 * <p/>
 * Licensed under the Apache License, Version 2.0 (the "License");
 *******************************************************************************/
package com.qinyeit.webapp.controller.management.system;

import com.qiniu.common.QiniuException;
import com.qiniu.common.Zone;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;
import com.qiniu.util.StringMap;
import com.qinyeit.webapp.controller.management.BaseManagementController;
import com.qinyetech.springstage.core.fileupload.service.WebFileUploadService;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * ClassName: ImageFileController <br/> 
 * Function: 图片上传,显示控制器 <br/>
 * date: 16/3/11 10:22 <br/> 
 *
 * @author wuqing
 * @version
 * @since JDK 1.7 
 */
@Controller
@RequestMapping("/management/image")
public class ImageFileUploadMerchantController extends BaseManagementController {

    @Resource(name = "fileUploadService")
    private WebFileUploadService webFileUploadService;

    //最大上传大小 字节为单位
    private long maxSize = 10*1024*1024L;

    //允许的文件内容类型
    private String[] allowedExtension = ArrayUtils.addAll(WebFileUploadService.IMAGE_EXTENSION, "mp3", "wma");

    @RequestMapping(value="/upload",method= RequestMethod.POST)
    @ResponseBody
    public Object fileUpload(HttpServletRequest request, @RequestParam(value = "file", required = false) MultipartFile file){
        Response res=uploadToQN(file);
        try {
            if(res!=null&& res.isOK()){
                StringMap stringMap=res.jsonToMap();
                return ok("上传成功", qnDomain+"/"+stringMap.get("hash"));
            }
        } catch (QiniuException e) {
            e.printStackTrace();
        }
        return fail("上传失败");
    }


    @Value("${qiniu.access_key:}")
    private String qnAccessKey;

    @Value("${qiniu.secret_key:}")
    private String qnSecretKey;

    @Value("${qiniu.bucket_name:}")
    private String qnBucketName;

    @Value("${qiniu.domain:}")
    private String qnDomain;


    public Response uploadToQN(MultipartFile file){
        //密钥配置
        Auth auth = Auth.create(qnAccessKey, qnSecretKey);

        log.info("------------->:{}",qnAccessKey);
        log.info("------------->:{}",qnSecretKey);

        //创建上传对象
        Configuration cfg = new Configuration(Zone.zone0());
        UploadManager uploadManager = new UploadManager(cfg);
        try {

            //调用put方法上传
            Response res = uploadManager.put(file.getBytes() , null, auth.uploadToken(qnBucketName));
            return res;
        } catch (QiniuException e) {
            Response r = e.response;
            // 请求失败时打印的异常的信息
            log.error(r.toString(),e);
        } catch (IOException e) {
            log.error("{}",e);
        }
        return null;
    }
}
